package servlet;

import DAO.ProductImageDAO;
import bean.Category;
import bean.Product;
import bean.ProductImage;
import org.apache.commons.fileupload.FileUploadException;
import util.ImageUtil;
import util.Page;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProductImageServlet extends BaseBackServlet {
    @Override
    public String add(HttpServletRequest request, HttpServletResponse response, Page page) {
        Map<String, String> map = new HashMap<>();
        String res = "";
        try {
            InputStream in = ImageUtil.upload(request, map);
//            System.out.println(in);
            ProductImage image = new ProductImage();
            String type = map.get("type");
            int pid = Integer.parseInt(map.get("pid"));
            Product product = productDAO.get(pid);
            image.setProduct(product);
            image.setType(type);
            productImageDAO.add(image);
//            System.out.println(category);
            int id = image.getId();
            ServletContext sc = request.getServletContext();
            String productsPath;
            String products_smallPath = "";
            String products_middlePath ="";
            if(type.equals("type_single"))
            {
                productsPath = sc.getRealPath("img/productSingle");
                products_smallPath = sc.getRealPath("img/productSingle_small");
                products_middlePath = sc.getRealPath("img/productSingle_middle");
            }
            else
            {
                productsPath = sc.getRealPath("img/productDetail");
            }
            File desc = new File(productsPath);
            if (!desc.exists()) {
                desc.mkdirs();
            }
            String fileName = id + ".jpg";
            File file = new File(desc, fileName);
            FileOutputStream out = new FileOutputStream(file);
            ImageUtil.copyFile(in, out);
            BufferedImage img = ImageUtil.change2jpg(file);
            ImageIO.write(img, "jpg", file);

            if(type.equals("type_single"))
            {
                File desc_small = new File(products_smallPath,fileName);
                File desc_middle = new File(products_middlePath,fileName);
                ImageUtil.resizeImage(file, 56, 56, desc_small);
                ImageUtil.resizeImage(file, 217, 190, desc_middle);
            }
            res =  "@admin_productImage_list?pid="+pid;
        } catch (FileUploadException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }

    @Override
    public String delete(HttpServletRequest request, HttpServletResponse respone, Page page) {
        int id = Integer.parseInt(request.getParameter("piid"));
        boolean result = false;
        String str = "";
        ProductImage image = productImageDAO.get(id);
        productImageDAO.delete(id);
        String type = image.getType();
        type=type.substring(type.indexOf("_")+1);
        type.replace(type.charAt(0),type.toUpperCase().charAt(0));
//        System.out.println(type);
        String path = request.getServletContext().getRealPath("img/product"+type);
        ImageUtil.delFile(path,id);
        if(type.equals(ProductImageDAO.type_single))
        {
            String path1 = request.getServletContext().getRealPath("img/product"+type+"_small");
            ImageUtil.delFile(path,id);
            String path2 = request.getServletContext().getRealPath("img/product"+type+"_middle");
            ImageUtil.delFile(path,id);

        }
        str = "@admin_productImage_list?cid="+image.getProduct().getCategory().getId()+"&pid="+image.getProduct().getId();
        return str;
    }

    @Override
    public String edit(HttpServletRequest request, HttpServletResponse response, Page page) {
        return null;
    }

    @Override
    public String update(HttpServletRequest request, HttpServletResponse response, Page page) {
        return null;
    }

    @Override
    public String list(HttpServletRequest request, HttpServletResponse response, Page page) {
        int start = page.getStart();
        int count = page.getCount();
        int pid = Integer.parseInt(request.getParameter("pid"));
        int total = productImageDAO.getTotal();
        page.setTotal(total);
//        System.out.println(start+"--"+count+"--"+total);
        Product product = productDAO.get(pid);
        List<ProductImage> images1 = productImageDAO.list(product,"type_single",start,count);
        List<ProductImage> images2 = productImageDAO.list(product,"type_detail",start,count);
        request.setAttribute("images1", images1);
        request.setAttribute("images2", images2);
        request.setAttribute("product",product);
        request.setAttribute("page", page);
        return "jsp/admin/listProductImage.jsp";
    }
}
